﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Negocio.Auxiliar;
using Negocio.Evento;


namespace Finalcountdown.Evento.Consultar
{
    public partial class consultarEvento : System.Web.UI.Page
    {
        private int paginaAtual = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                carregarDdl();
            }
        }

        private void carregarDdl()
        {
            var obNStatus = new NStatus();
            
            //Carregar status
            ddlStatus.DataSource = obNStatus.pegarTodosStatus();
            ddlStatus.DataBind();
            ddlStatus.Items.Insert(0, new ListItem("Selecionar o Status", "0"));
        }

        protected void lnk_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Evento/Incluir/incluirEvento.aspx");
        }

        protected void lkbPesquisar_Click(object sender, EventArgs e)
        {
            carregarGrid();
        }

        protected void grdResEvento_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            paginaAtual = e.NewPageIndex;
            carregarGrid();
        }

        protected void grdResEvento_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "editar":
                    var idEvento = e.CommandArgument.ToString();
                    Response.Redirect("~/Evento/Editar/editarEvento.aspx?id=" + idEvento);
                    break;
                case "remover":
                    var idEventoRemover = e.CommandArgument.ToString();
                    hdfIdEventoRemover.Value = idEventoRemover;
                    ModalPopupRemover.Show();
                    break;
                case "acompanhar":
                    var idEventoAcompanhar = e.CommandArgument.ToString();
                    Response.Redirect("~/Evento/Acompanhar/acompanharEvento.aspx?id=" + idEventoAcompanhar);
                    break;
            }
        }

        protected void lkbSim_Click(object sender, EventArgs e)
        {
            var idEvento = Convert.ToInt32(hdfIdEventoRemover.Value);
            var obNEvento = new NEvento();
            if(obNEvento.Remover(idEvento))
            {
                divMsnExclusaoSucesso.Visible = true;
            }else
            {
                divMsnExclusaoSucesso.Visible = false;
            }
            carregarGrid();
        }

        protected void lkbNao_Click(object sender, EventArgs e)
        {
            ModalPopupRemover.Hide();
        }

        private void carregarGrid()
        {
            var obNEvento = new NEvento();
            var obListEventos = obNEvento.pegarEventos(txtNmEvento.Text, txtDtEvento.Text, Convert.ToInt32(ddlStatus.SelectedValue));

            if (obListEventos.Tables[0].Rows.Count != 0)
            {
                divMsnPesquisaSucesso.Visible = true;
                lblNumEncontrados.Text = "Foram encontrados " + obListEventos.Tables[0].Rows.Count.ToString() + " registros.";
                divMsnPesquisaAtencao.Visible = false;
                divMsnExclusaoSucesso.Visible = false;
            }
            else
            {
                divMsnExclusaoSucesso.Visible = false;
                divMsnPesquisaSucesso.Visible = false;
                divMsnPesquisaAtencao.Visible = true;
            }

            grdResEvento.PageSize = 15;
            grdResEvento.AllowPaging = true;
            grdResEvento.PageIndex = paginaAtual;
            grdResEvento.DataSource = obListEventos;
            grdResEvento.DataBind();
        }

        protected void grdResEvento_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var obNStatus = new NStatus();
                obNStatus.pegarStatusPorId(Convert.ToInt32(e.Row.Cells[2].Text));

                if(e.Row.Cells[2].Text == "2")
                {
                    var lkbAcompanhar = (LinkButton)e.Row.FindControl("lkbAcompanhar");
                    lkbAcompanhar.Enabled = false;

                    var lkbRemover = (LinkButton)e.Row.FindControl("lkbRemover");
                    lkbRemover.Enabled = false;
                }

                e.Row.Cells[2].Text = obNStatus.dsStatus;

                e.Row.Cells[1].Text = Convert.ToDateTime(e.Row.Cells[1].Text).ToShortDateString();

            }
        }

    }
}